package com.bjx.gateway.config;


import io.swagger.annotations.ApiOperation;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @desc: swagger配置
 * @author: liushuai
 * @date: 2022/6/30 21:44
 */
@Configuration
@EnableOpenApi
public class BjxSwaggerConfiguration implements EnvironmentAware {
    private String applicationName;

    private String applicationDescription;
    @Bean
    public Docket createRestApi() {
       return  new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                 .enable(true)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .paths(PathSelectors.any())
                .build()
                .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
                .globalResponses(HttpMethod.POST, getGlobalResponseMessage())
               .securitySchemes(securitySchemes())
               .securityContexts(securityContexts()).pathMapping("/");

    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private List<SecurityScheme> securitySchemes()
    {
        List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts()
    {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .operationSelector(o -> o.requestMappingPattern().matches("/.*"))
                        .build());
        return securityContexts;
    }

    /**
     * 默认的全局鉴权策略
     *
     * @return
     */
    private List<SecurityReference> defaultAuth()
    {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }


    /**
     * 生成接口信息，包括标题，联系人等
     * @param
     * @return: springfox.documentation.service.ApiInfo
     * @Author: liushuai
     * @Date: 2022/6/23 17:17
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName +"接口文档")
                .description(applicationDescription)
                .contact(new Contact("北极星","","15244182718@163.com"))
                .version("1.0")
                .build();
    }
    /**
     * 封装通用相应信息
     * @param
     * @return: java.util.List<springfox.documentation.service.Response>
     * @Author: liushuai
     * @Date: 2022/6/23 17:31
     */
    private List<Response> getGlobalResponseMessage() {
        List<Response> responseList=new ArrayList<>();
        responseList.add(new ResponseBuilder().code("404").description("未找到资源").build());
        return responseList;
    }



    @Override
    public void setEnvironment(Environment environment) {
        this.applicationDescription = environment.getProperty("swagger.description");
        this.applicationName = environment.getProperty("swagger.name");
    }
}
